﻿using HamcoWcs.Contract.ContractCommon;
using HamcoWcs.Contract.ContractMes;
using HamcoWcs.Contract.ContractPcon.IPconDbMsgParsers;
using HamcoWcs.DAL;
using HamcoWcs.Entitys.SubTasks;
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Models.Devices;
using HamcoWcs.Models.TibcoMessage;
using HamcoWcs.Service.Common;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HamcoWcs.Service.Srm.SrmTaskParsers
{
    [AutoInject(typeof(ISrmParser), InjectType.Scope)]
    public abstract class SrmParserBase : ISrmParser
    {
        protected readonly WcsDbContext _wcsDbContext;
        protected readonly IMesCommunicateService _mesCommunicateService;
        protected readonly ILoggerService _logger;
        protected readonly ILocationService _locationService;

        public SrmParserBase(WcsDbContext wcsDbContext, ILocationService locationServce, IMesCommunicateService mesCommunicateService, ILoggerService logger)
        {
            _wcsDbContext = wcsDbContext;
            _mesCommunicateService = mesCommunicateService;
            _locationService = locationServce;
            _logger = logger;
        }

        public abstract EnumSrmRequest RequestType { get; }

        public abstract void Deal(SrmDto srm, SrmPlc srmPlc);

        protected virtual SubTask GetTargetTask(SrmDto srm)
        => _wcsDbContext.SubTasks
                        .Where(x => x.TaskStatus == EnumSubTaskStatus.OnProgress.ToString())
                        .Where(x => x.UnitId == srm.UnitId)
                        .Include(x => x.MainTask)
                        .FirstOrDefault();


  

        protected virtual SubTask GetSrmTask(SrmDto srm)
        => _wcsDbContext.SubTasks
                        .Where(x => x.TaskStatus == EnumSubTaskStatus.OnProgress.ToString())
                        .Where(x => x.PlcName == srm.SrmName)
                        .Include(x => x.MainTask)
                        .Where(x => x.MainTask.TaskStatus == EnumMainTaskStatus.OnProgress.ToString())
                        .FirstOrDefault();

    }
}
